package cn.lbd.kill.server.controller;

import cn.lbd.kill.model.entity.ItemKill;
import cn.lbd.kill.server.service.ItemService;
import cn.lbd.kill.server.service.impl.ItemServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ItemController {
    private static Logger log = LoggerFactory.getLogger(ItemServiceImpl.class);
    @Autowired
    private ItemService killService;
    //配置KillController统一前缀
    private static final String prefix = "item";

    /**
     * 获取待秒杀商品列表
     *
     * @return
     */
    @RequestMapping(value = {"/", "/index", prefix + "/list", prefix + "/index.html"}, method = RequestMethod.GET)
    public String getKillItems(Model model) {
        try {
            List<ItemKill> killItems = killService.getKillItems();
            model.addAttribute("list", killItems);
            log.info("获取待秒杀商品列表-数据：{}", killItems);
        } catch (Exception e) {
            log.error("获取待秒杀商品列表-发生异常： ", e.fillInStackTrace());
            return "redirect:/base/error";
        }
        return "list";
    }

    @RequestMapping(value = prefix + "/detail/{id}",method = RequestMethod.GET)
    public String getKillItemsInfo(@PathVariable Integer id,Model model) {
        if (id == null || id <= 0) {
            return "redirect:/base/error";
        }
        try {
            ItemKill killItemsInfo = killService.getKillItemsInfo(id);
            model.addAttribute("detail",killItemsInfo);
            log.info("获取待秒杀商品详情-数据：{}", killItemsInfo);
        } catch (Exception e) {
            log.error("获取待秒杀商品详情-发生异常： ", e.fillInStackTrace());
            return "redirect:/base/error";
        }
        return "info";
    }

}
